﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Tools;

namespace Punia
{
    [CommandAttribute(false)]
    public partial class alter_authorization : BaseForm
    {
        public alter_authorization()
            : base()
        {
        }

        public alter_authorization(DbContext dbContext)
            : base(dbContext)
        {
            InitializeComponent();
        }

        protected override void Write()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("ALTER AUTHORIZATION ON");

            if (cbObject.SelectedIndex > 0)
            {
                sb.Append("   " + cbObject.Text + "::");
                if (lbObject.Items.Count > 0)
                    sb.Append(lbObject.Text);

                sb.AppendLine();
            }

            if (cbSchema.Checked)
                sb.AppendLine("   TO SCHEMA OWNER");

            sb.AppendLine("   TO [" + lbPrincipal.Text + "]");

            tbCommand.Text = sb.ToString();
        }

        private void cbSchema_CheckedChanged(object sender, EventArgs e)
        {
            if (cbSchema.Checked)
            {
                lblPrincipal.Enabled = false;
                cbObject.Enabled = false;
            }
            else
            {
                lblPrincipal.Enabled = true;
                cbObject.Enabled = true;
            }

            Write();
        }

        private void lbPrincipal_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbObject_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbObject.Text == "Endpoint")
            {
                lbPrincipal.DataSource = Utility.logins(dbContext);
                lbPrincipal.DisplayMember = "Name";
            }
            else if (cbObject.Text != string.Empty)
            {
                lbPrincipal.DataSource = Utility.principals(dbContext);
                lbPrincipal.DisplayMember = "Name";
            }
            else
            {
                lbPrincipal.DataSource = null;
            }

            switch (cbObject.Text)
            {
                case "Message type":
                    lbObject.DataSource = Utility.message_types_select(dbContext);
                    break;
                case "Contract":
                    lbObject.DataSource = Utility.contracts(dbContext);
                    break;
                case "Service":
                    lbObject.DataSource = Utility.services_select(dbContext);
                    break;
                case "Certificate":
                    lbObject.DataSource = Utility.certificates_select(dbContext);
                    break;
                case "Route":
                    lbObject.DataSource = Utility.routes_select(dbContext);
                    break;
                case "Endpoint":
                    lbObject.DataSource = Utility.endpoints_select(dbContext);
                    break;
                default:
                    lbObject.DataSource = null;
                    break;
            }

            lbObject.DisplayMember = "Name";

            Write();
        }

        private void lbObject_SelectedIndexChanged(object sender, EventArgs e)
        {

            Write();
        }
    }
}